﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.PeerResolvers;
using System.Text;
using log4net;

namespace WCFPeerResolver
{
    /// <summary>
    /// This is a proxy that makes it easy to change the backend of the resolver.
    /// </summary>
    /// <remarks>
    /// See the example app.config included in the project for usage.
    /// </remarks>
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false,
        InstanceContextMode = InstanceContextMode.Single)]
    public class CustomPeerResolverProxy : IPeerResolverContract
    {
        private readonly IPeerResolverContract m_Resolver;
        private readonly ILog m_Log = LogManager.GetLogger("ResolverProxy");

        public CustomPeerResolverProxy(IPeerResolverContract resolver)
        {
            m_Resolver = resolver;
        }

        #region IPeerResolverContract Members

        public ServiceSettingsResponseInfo GetServiceSettings()
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("GetServerSettings called.");
            return m_Resolver.GetServiceSettings();
        }

        public RefreshResponseInfo Refresh(RefreshInfo refreshInfo)
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("Refresh called.");
            return m_Resolver.Refresh(refreshInfo);
        }

        public RegisterResponseInfo Register(RegisterInfo registerInfo)
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("Register called.");
            return m_Resolver.Register(registerInfo);
        }

        public ResolveResponseInfo Resolve(ResolveInfo resolveInfo)
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("Resolve called.");
            return m_Resolver.Resolve(resolveInfo);
        }

        public void Unregister(UnregisterInfo unregisterInfo)
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("Unregister called.");
            m_Resolver.Unregister(unregisterInfo);
        }

        public RegisterResponseInfo Update(UpdateInfo updateInfo)
        {
            if (m_Log.IsDebugEnabled)
                m_Log.Debug("Update called.");
            return m_Resolver.Update(updateInfo);
        }

        #endregion
    }
}
